package com.txzt.gaea.business.fund.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.business.fund.domain.model.FundBudget;
import com.txzt.gaea.business.fund.service.IFundBudgetService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.enums.FundStatus;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/fund/budget")
public class FundBudgetController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(FundBudgetController.class);

    @Autowired
    private IFundBudgetService fundBudgetService;

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param fundBudget
     * @return
     */
    @GetMapping(value = "/list")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, FundBudget fundBudget) {
        logger.info(fundBudget.toString());
        PageQuery<FundBudget> pageList = fundBudgetService.pageQuery(new PageQuery<>(pageNumber, pageSize, fundBudget));
        pageList.getList().forEach(item -> {
            //翻译创建人
            item.setCreateName(TranslateUtil.getUserName(item.getCreateBy()));
        });
        return RtnInfo.success(pageList);
    }

    @GetMapping(value = "/listHandled")
    public RtnInfo<?> listHandled() {
        return RtnInfo.success(fundBudgetService.getHandledList());
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public RtnInfo<?> queryById(@RequestParam Long id) {
        FundBudget fundBudget = fundBudgetService.selectFundBudgetById(id);
        if (null == fundBudget) {
            return RtnInfo.SERVER_ERROR;
        }else {
            return RtnInfo.success(fundBudget);
        }

    }

    /**
     * 新增
     * @param fundBudget
     * @return
     */
    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> add(@RequestBody FundBudget fundBudget) {
        int count = fundBudgetService.addFundBudget(fundBudget);
        if (9 == count) {
            return RtnInfo.error(9,"预算名称已存在！");
        }
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 提交
     * @param id
     * @return
     */
    @GetMapping(value = "/submit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> submit(@RequestParam Long id) {
        int count = fundBudgetService.submitFundBudget(id);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 编辑
     * @param fundBudget
     * @return
     */
    @PutMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> edit(@RequestBody FundBudget fundBudget) {
        int count = fundBudgetService.editFundBudget(fundBudget);
        if (9 == count) {
            return RtnInfo.error(9,"预算名称已存在！");
        }
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 处理
     * @param fundBudget
     * @return
     */
    @PutMapping(value = "/handle")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> handle(@RequestBody FundBudget fundBudget) {
        fundBudget.setFundStatus(FundStatus.FINISH.getCode());
        fundBudget.setRemainFund(fundBudget.getFundTotal());
        int count = fundBudgetService.updateById(fundBudget);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        FundBudget fundBudget = new FundBudget();
        fundBudget.setId(id);
        fundBudget.setIsDelete(IsDeleteEnum.DELETE.getCode());
        int count = fundBudgetService.updateById(fundBudget);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

}
